﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace TicketSellingServer
{
    internal static class FlightsDatabase
    {
        private static List<Flight> flights;

        public static void LoadFlights(Stream s)
        {
            flights = new List<Flight>();

            using (var reader = new StreamReader(s))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    var items = line.Split(' ');

                    flights.Add(new Flight
                    {
                        FlightId = items[0],
                        From = items[1],
                        To = items[2],
                        Date = Flight.DateFromString(items[3]),
                        AvailableSeats = int.Parse(items[4]),
                        Price = int.Parse(items[5]),
                    });
                }
            }
        }

        public static Flight GetFlight(string flightId, DateTime date)
        {
            return Flights.FirstOrDefault(f => f.FlightId == flightId && f.Date == date);
        }

        public static IEnumerable<Flight> Flights
        {
            get { return flights; }
        }
    }
}
